package com.regedit.aplicacion.controllers;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.util.Log;

import com.regedit.aplicacion.communication.WebServiceConsumer;
import com.regedit.aplicacion.dao.EntityDao;

public class CarroController {

	private Context mContext;

	public CarroController(Context context) {
		mContext = context;
	}

	public JSONObject getCarros() {
		WebServiceConsumer webService = WebServiceConsumer
				.getInstance(mContext);
		String jsonString = webService.getCarros();
		JSONObject json = null;
		try {
			json = new JSONObject(jsonString);
			Log.e("JSon", json.toString());
		} catch (JSONException e) {
			Log.e("JSon", e.getMessage());
		}
		return json;
	}

	public boolean insertAuto(JSONObject auto) {
		WebServiceConsumer webService = WebServiceConsumer
				.getInstance(mContext);
		String jsonString = webService.getCarros();

		return true;
	}

	public Cursor obtenerCarrosConContentProvider(String nombre){
		String selection = EntityDao.COL_TITLE + " LIKE ?";
		String[] selectionArgs = new String[] { "%" + nombre + "%" };
		return mContext.getContentResolver().query(EntityDao.CONTENT_URI,
				null, selection, selectionArgs, null);
	}
	
	public Cursor obtenerCarrosConContentProviderMix(String nombre){

		MatrixCursor extras = new MatrixCursor(new String[] { "_id",
				"title" });
		extras.addRow(new String[] { "-1", "Seleccione Carro"});
		
		String selection = EntityDao.COL_TITLE + " LIKE ?";
		String[] selectionArgs = new String[] { "%" + nombre + "%" };

		Cursor cursor = mContext.getContentResolver().query(EntityDao.CONTENT_URI,
				null, selection, selectionArgs, null);

		Cursor[] cursors = { extras, cursor };
		Cursor extendedCursor = new MergeCursor(cursors);
		return extendedCursor;	
	}
}
